//
//  JHSearchWordViewController.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/5/27.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHSearchWordViewController.h"
#import "JHSearchWordCell.h"
#import "JHExplainDetailViewController.h"

@interface JHSearchWordViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextField *enterText;
@property (weak, nonatomic) IBOutlet UIButton *transferBtn;

@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation JHSearchWordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"翻译";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 60;
    self.dataArray = [NSMutableArray array];
    
    [self getTranslateData];
}

- (void)getTranslateData
{
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:@{@"w":self.word}];
    [self deptNameInputShouldChinese] ?
    [dic addEntriesFromDictionary:@{@"f":@"zh", @"t":@"en"}] :
    [dic addEntriesFromDictionary:@{@"f":@"en", @"t":@"zh"}];
    
    [DJHttpTool postWithURL:@"http://fy.iciba.com/ajax.php?a=fy" params:dic success:^(id json) {
        if (json[@"status"])
        {
            self.enterText.text = @"";
            NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:json[@"content"]];
            [dic setValue:self.word forKey:@"word"];
            NSString * timestamp = [DJTool getNowTimeTimestamp];
            [dic setValue:timestamp forKey:@"timestamp"];
            [dic setValue:json[@"status"] forKey:@"status"];
            [self.dataArray addObject:dic];
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {

    }];
}

- (BOOL)deptNameInputShouldChinese
{
    NSString *regex = @"[\u4e00-\u9fa5]+";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if ([pred evaluateWithObject:self.word]) {
        return YES;
    }
    return NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, 20)];
    view.backgroundColor = JKColor(239, 239, 244);
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JKScreenW, 10)];
    view.backgroundColor = JKColor(239, 239, 244);
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    JHSearchWordCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:@"JHSearchWordCell" owner:self options:nil].firstObject;
    }
    cell.dataDic = self.dataArray[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JHExplainDetailViewController * detail = [JHExplainDetailViewController new];
    detail.explainWord = self.dataArray[indexPath.section][@"word"];
    detail.wordDic     = self.dataArray[indexPath.section];
    detail.type = 1;
    [self.navigationController pushViewController:detail animated:YES];
}

- (IBAction)transferBtnClick:(id)sender
{
    [self.enterText resignFirstResponder];
    if (self.enterText.text.length == 0)
    {
        return;
    }
    self.word = self.enterText.text;
    [self getTranslateData];
}


@end
